package com.jz.netthings.analysis.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jz.netthings.analysis.bean.ProviderRoundBase;
import com.jz.netthings.analysis.bean.link.LinkAnalysisProviderSupport;
import com.jz.netthings.analysis.bean.link.NurseLinkProviderStatistics;
import com.jz.netthings.analysis.mapper.NurseLinkProviderStatisticsMapper;
import com.jz.netthings.analysis.mapper.ProviderRoundBase2Mapper;
import com.jz.netthings.analysis.mapper.XyDevelopAnalysisMapper;
import com.jz.netthings.analysis.service.NurseLinkProviderStatisticsService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jz.netthings.analysis.service.ProviderRoundBaseService;
import com.jz.netthings.application.providerCollection.bean.ProviderCollection;
import com.jz.netthings.application.providerCollection.mapper.mysql.ProviderCollectionMapper;
import com.jz.netthings.bloodsources.mapper.XyLinkMapper;
import com.jz.netthings.exception.AdscmSystemException;
import com.jz.netthings.support.AdscmPage;
import com.jz.netthings.utils.AdscmBaseUtils;
import com.jz.netthings.utils.AdscmDateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杨云云
 * @since 2019-08-29
 */
@Service
public class NurseLinkProviderStatisticsServiceImpl extends ServiceImpl<NurseLinkProviderStatisticsMapper, NurseLinkProviderStatistics> implements NurseLinkProviderStatisticsService {

    Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Autowired
    XyLinkMapper xyLinkMapper;
    @Autowired
    NurseLinkProviderStatisticsMapper nurseLinkProviderStatisticsMapper;
    @Autowired
    XyDevelopAnalysisMapper xyDevelopAnalysisMapper;
    @Autowired
    ProviderRoundBase2Mapper providerRoundBase2Mapper;
    @Autowired
    ProviderCollectionMapper providerCollectionMapper;
    @Autowired
    ProviderRoundBaseService providerRoundBaseService;

    @Autowired
    RedisTemplate<Object , Object> objectRedisTemplate;

    @Override
    public void loadNurseLinkProviderStatistics() throws AdscmSystemException {
        
        logger.info("装载护士链接的综合统计信息。。。。");

        List<Map<String, Object>> linkNursePCount = xyLinkMapper.getLinkNursePCount();

        if (AdscmBaseUtils.getInstance().listIsNotEmpty(linkNursePCount)){
            Map<Integer,Map<String,List<Long>>> cacheMap = new HashMap<>();
            for (Map<String, Object> map : linkNursePCount) {
                Long useNurseId = (Long) map.get("useNurseId");
                String useNurseName = (String) map.get("useNurseName");
                Long linkProviderCount = (Long) map.get("linkProviderCount");

                logger.info("装载护士【"+useNurseName+"】的链接综合统计信息...");

                //在每个类型的浆员集合查询出来后，存入到缓存中，供分类查询使用
                Map<String,List<Long>> cacheTypeMap = new HashMap<>();

                NurseLinkProviderStatistics statistics = nurseLinkProviderStatisticsMapper.selectById(useNurseId);

                boolean isInsert = true;

                if (statistics != null){
                    isInsert = false;
                }else {
                    statistics = new NurseLinkProviderStatistics();
                    statistics.setNurseId(useNurseId.intValue());
                    statistics.setNurseName(useNurseName);
                }

                //注册浆员总人数
                statistics.setAllProviderNum(linkProviderCount.intValue());

                //护士链接的浆员ID集合
                List<Long> linkNursePList = xyLinkMapper.getLinkNursePList(useNurseId.intValue());

                //链接的所有浆员列表
                cacheTypeMap.put("allLinkProviderList",linkNursePList);


                Wrapper<ProviderCollection> collWrapper = new EntityWrapper<>();
//                collWrapper.in("providerNo",linkNursePList);
                collWrapper.eq("isDeal",1);
                collWrapper.gt("totalPlasmaNum",590);
                collWrapper.orderBy("collectionDate",true);

                Date now = new Date();
                String nowDateStr = AdscmDateUtils.getNowOnlyDateFomat1(now);
                String lastYearNowDateStr = AdscmDateUtils.getNowOnlyDateFomat1(now,-1);
                int year = AdscmDateUtils.getTheYear(now);

                String beginTime = (year - 1) + "-01-01 00:00:00";
                String endTime = (year - 1) + "-12-31 23:59:59";

                Wrapper<ProviderCollection> tempWrapper = collWrapper.clone().in("providerNo",linkNursePList).between("collectionDate",beginTime,endTime);

                /*
                 * 去年献浆总量/袋
                 */
                Integer lastYearCollNum = providerCollectionMapper.selectCount(tempWrapper);
                statistics.setLastYearCollNum(lastYearCollNum);
                /*
                 * 当期进站人数
                 */
                beginTime = year + "-01-01 00:00:00";
                endTime = nowDateStr + " 23:59:59";
                Integer currentTimePNum = nurseLinkProviderStatisticsMapper.getProviderComedPCount(linkNursePList,beginTime,endTime);
                statistics.setCurrentTimePNum(currentTimePNum);
                /*
                 * 同期进站人数
                 */
                beginTime = (year - 1) + "-01-01 00:00:00";
                endTime = lastYearNowDateStr + " 23:59:59";
                Integer lastTimePNum = nurseLinkProviderStatisticsMapper.getProviderComedPCount(linkNursePList,beginTime,endTime);
                statistics.setLastTimePNum(lastTimePNum);
                /*
                 * 当期献浆量/袋
                 */
                beginTime = year + "-01-01 00:00:00";
                endTime = nowDateStr + " 23:59:59";
                tempWrapper = collWrapper.clone().in("providerNo",linkNursePList).between("collectionDate",beginTime,endTime);
                Integer currentTimeCollNum = providerCollectionMapper.selectCount(tempWrapper);
                statistics.setCurrentTimeCollNum(currentTimeCollNum);
                /*
                 * 同期献浆量/袋
                 */
                beginTime = (year - 1) + "-01-01 00:00:00";
                endTime = lastYearNowDateStr + " 23:59:59";
                tempWrapper = collWrapper.clone().in("providerNo",linkNursePList).between("collectionDate",beginTime,endTime);
                Integer lastTimeCollNum = providerCollectionMapper.selectCount(tempWrapper);
                statistics.setLastTimeCollNum(lastTimeCollNum);
                /*
                 * 当期浆量和同期献浆量的差
                 */
                Integer collNumDiff = currentTimeCollNum - lastTimeCollNum;
                statistics.setCollNumDiff(collNumDiff);

                beginTime = year + "-01-01 00:00:00";
                endTime = nowDateStr + " 23:59:59";
                //当期新增的浆员ID集合
                List<Long> thisTimeCreateList = nurseLinkProviderStatisticsMapper.getProviderCreateList(linkNursePList, beginTime, endTime);
                beginTime = (year - 1) + "-01-01 00:00:00";
                endTime = lastYearNowDateStr + " 23:59:59";
                List<Long> lastTimeCreateList = nurseLinkProviderStatisticsMapper.getProviderCreateList(linkNursePList, beginTime, endTime);
                //今年新浆员
                cacheTypeMap.put("thisTimeCreateList",thisTimeCreateList);
                /*
                 * 当期新增人数
                 */
                Integer currentTimeAddPNum = thisTimeCreateList.size();
                statistics.setCurrentTimeAddPNum(currentTimeAddPNum);
                /*
                 * 同期新增人数
                 */
                Integer lastTimeAddPNum = lastTimeCreateList.size();
                statistics.setLastTimeAddPNum(lastTimeAddPNum);
                /*
                 * 当期新增总浆量/袋
                 */
                if (thisTimeCreateList.size() > 0){
                    beginTime = year + "-01-01 00:00:00";
                    endTime = nowDateStr + " 23:59:59";
                    tempWrapper = collWrapper.clone().in("providerNo",thisTimeCreateList).between("collectionDate",beginTime,endTime);
                    Integer currentTimeAddCollNum = providerCollectionMapper.selectCount(tempWrapper);
                    statistics.setCurrentTimeAddCollNum(currentTimeAddCollNum);
                }
                /*
                 * 同期新增总浆量/袋
                 */
                if (lastTimeCreateList.size() > 0) {
                    beginTime = (year - 1) + "-01-01 00:00:00";
                    endTime = lastYearNowDateStr + " 23:59:59";
                    tempWrapper = collWrapper.clone().in("providerNo", lastTimeCreateList).between("collectionDate", beginTime, endTime);
                    Integer lastTimeAddCollNum = providerCollectionMapper.selectCount(tempWrapper);
                    statistics.setLastTimeAddCollNum(lastTimeAddCollNum);
                }

                /*
                 * 当期与同期新增人数差/人数
                 */
                Integer addNumDiff = currentTimeAddPNum - lastTimeAddPNum;
                statistics.setAddNumDiff(addNumDiff);

                //查询链接了的浆员，今年进站的浆员集合
                beginTime = year + "-01-01 00:00:00";
                endTime = nowDateStr + " 23:59:59";
                List<Long> thisTimeComedList = nurseLinkProviderStatisticsMapper.getProviderComedPList(linkNursePList, beginTime, endTime);
                //今年已进站
                cacheTypeMap.put("thisTimeComedList",thisTimeComedList);

                //查询链接了的浆员，去年进站的浆员集合
                beginTime = (year - 1) + "-01-01 00:00:00";
                endTime = (year - 1) + "-12-31 23:59:59";
                List<Long> lastYearComedList = nurseLinkProviderStatisticsMapper.getProviderComedPList(linkNursePList, beginTime, endTime);

                //查询链接了的浆员，去年同期进站的浆员集合
                beginTime = (year - 1) + "-01-01 00:00:00";
                endTime = lastYearNowDateStr + " 23:59:59";
                List<Long> lastYearNowComedList = nurseLinkProviderStatisticsMapper.getProviderComedPList(linkNursePList, beginTime, endTime);

                //查询链接了的浆员，前年进站的浆员集合
                beginTime = (year - 2) + "-01-01 00:00:00";
                endTime = (year - 2) + "-12-31 23:59:59";
                List<Long> lastLastYearComedList = nurseLinkProviderStatisticsMapper.getProviderComedPList(linkNursePList, beginTime, endTime);



                //去年没来的浆员（总的浆员集合里面，去掉了去年来了的）
                List<Long> lastYearNotComedList = (List<Long>) AdscmBaseUtils.getInstance().removeList(linkNursePList,lastYearComedList);

                //去年没来，今年来了的浆员集合 = 去年没来的浆员集合 和 今年来了的浆员集合 的 交集
                List<Long> fusuPList = (List<Long>) AdscmBaseUtils.getInstance().retainList(lastYearNotComedList,thisTimeComedList);
                //复苏浆员
                cacheTypeMap.put("fusuPList",fusuPList);
                /*
                 * 当期复苏献浆员总人数（去年没来，今年来了）
                 */
                Integer currentTimeFuSuPNum = fusuPList.size();
                statistics.setCurrentTimeFuSuPNum(currentTimeFuSuPNum);
                /*
                 * 当期复苏浆员献浆总量/袋
                 */
                if (fusuPList.size() > 0) {
                    tempWrapper = collWrapper.clone().in("providerNo", fusuPList);//.between("collectionDate",beginTime,endTime);
                    Integer currentTimeFuSuCollNum = providerCollectionMapper.selectCount(tempWrapper);
                    statistics.setCurrentTimeFuSuCollNum(currentTimeFuSuCollNum);
                }


                //前年没来的浆员（总的浆员集合里面，去掉了前年来了的）
                List<Long> lastLastYearNotComedList = (List<Long>) AdscmBaseUtils.getInstance().removeList(linkNursePList,lastLastYearComedList);

                //前年没来，去年来了的浆员集合 = 前年没来的浆员集合 和 去年同期来了的浆员集合 的 交集
                List<Long> lastFusuPList = (List<Long>) AdscmBaseUtils.getInstance().retainList(lastLastYearNotComedList,lastYearNowComedList);


                /*
                 * 同期复苏浆员总人数
                 */
                Integer lastTimeFuSuNum = lastFusuPList.size();
                statistics.setLastTimeFuSuNum(lastTimeFuSuNum);
                /*
                 * 同期复苏浆员浆量
                 */
                if (lastFusuPList.size() > 0) {
                    tempWrapper = collWrapper.clone().in("providerNo", lastFusuPList);//.between("collectionDate",beginTime,endTime);
                    Integer lastTimeFuSuCollNum = providerCollectionMapper.selectCount(tempWrapper);
                    statistics.setLastTimeFuSuCollNum(lastTimeFuSuCollNum);
                }



                //当期忠诚浆员，去年来了的浆员集合 和 今年来了的浆员集合 的 交集
                List<Long> nowZcPList = (List<Long>) AdscmBaseUtils.getInstance().retainList(lastYearComedList,thisTimeComedList);
                //同期忠诚浆员，前年来了的浆员集合 和 去年年来了的浆员集合 的 交集
                List<Long> lastNowZcPList = (List<Long>) AdscmBaseUtils.getInstance().retainList(lastLastYearComedList,lastYearNowComedList);

                //忠诚浆员
                cacheTypeMap.put("nowZcPList",nowZcPList);

                /*
                 * 忠诚献浆员当期总人数
                 */
                Integer currentTimeZCPNum = nowZcPList.size();
                statistics.setCurrentTimeZCPNum(currentTimeZCPNum);
                /*
                 * 忠诚献浆员同期期总人数
                 */
                Integer lastTimeZCPNum = lastNowZcPList.size();
                statistics.setLastTimeZCPNum(lastTimeZCPNum);
                /*
                 * 忠诚献浆员当期献浆总量
                 */
                Integer currentTimeZCCollNum = 0;
                if (nowZcPList.size() > 0) {
                    tempWrapper = collWrapper.clone().in("providerNo", nowZcPList);//.between("collectionDate",beginTime,endTime);
                    currentTimeZCCollNum = providerCollectionMapper.selectCount(tempWrapper);
                    statistics.setCurrentTimeZCCollNum(currentTimeZCCollNum);
                }
                /*
                 * 忠诚献浆员同期献浆总量
                 */
                Integer lastTimeZCCollNum = 0;
                if (lastNowZcPList.size() > 0) {
                    tempWrapper = collWrapper.clone().in("providerNo", lastNowZcPList);
                    lastTimeZCCollNum = providerCollectionMapper.selectCount(tempWrapper);
                    statistics.setLastTimeZCCollNum(lastTimeZCCollNum);
                }
                /*
                 * 当期与同期忠诚浆员浆量差/袋
                 */
                Integer zcCollNumDiff = currentTimeZCCollNum - lastTimeZCCollNum;
                statistics.setZcCollNumDiff(zcCollNumDiff);

                //当期未进站浆员（总的浆员集合里面，去掉了今年来了的）
                List<Long> currentTimeNotComedPList = (List<Long>) AdscmBaseUtils.getInstance().removeList(linkNursePList,thisTimeComedList);
                //今年未进站浆员
                cacheTypeMap.put("currentTimeNotComedPList",currentTimeNotComedPList);
                /*
                 * 当期未进站总人数
                 */
                Integer currentTimeNotComedPNum = currentTimeNotComedPList.size();
                statistics.setCurrentTimeNotComedPNum(currentTimeNotComedPNum);
                /*
                 * 当期未进站总人数去年献浆总量袋
                 */
                if (currentTimeNotComedPList.size() > 0) {
                    beginTime = (year - 1) + "-01-01 00:00:00";
                    endTime = (year - 1) + "-12-31 23:59:59";
                    tempWrapper = collWrapper.clone().in("providerNo", currentTimeNotComedPList).between("collectionDate", beginTime, endTime);
                    Integer currentTimeNotComedPLastYearCollNum = providerCollectionMapper.selectCount(tempWrapper);
                    statistics.setCurrentTimeNotComedPLastYearCollNum(currentTimeNotComedPLastYearCollNum);
                }

                //休眠：去年来了和今年没来的交集
                List<Long> xiuMianPList = (List<Long>) AdscmBaseUtils.getInstance().retainList(lastYearComedList,currentTimeNotComedPList);
                //休眠浆员
                cacheTypeMap.put("xiuMianPList",xiuMianPList);
                /*
                 * 休眠浆员总人数（去年来了，今年没来）
                 */
                Integer xiuMianPNum = xiuMianPList.size();
                statistics.setXiuMianPNum(xiuMianPNum);
                /*
                 * 休眠浆员去年献浆总量/袋
                 */
                Integer lastYearXiuMianCollNum = 0;
                if (xiuMianPList.size() > 0) {
                    beginTime = (year - 1) + "-01-01 00:00:00";
                    endTime = (year - 1) + "-12-31 23:59:59";
                    tempWrapper = collWrapper.clone().in("providerNo", xiuMianPList).between("collectionDate", beginTime, endTime);
                    lastYearXiuMianCollNum = providerCollectionMapper.selectCount(tempWrapper);
                }
                statistics.setLastYearXiuMianCollNum(lastYearXiuMianCollNum);
                /*
                 * 休眠浆员同期献浆总量/袋
                 */
                Integer lastTimeXiuMianCollNum = 0;
                if (xiuMianPList.size() > 0) {
                    beginTime = (year - 1) + "-01-01 00:00:00";
                    endTime = lastYearNowDateStr + " 23:59:59";
                    tempWrapper = collWrapper.clone().in("providerNo", xiuMianPList).between("collectionDate", beginTime, endTime);
                    lastTimeXiuMianCollNum = providerCollectionMapper.selectCount(tempWrapper);
                }
                statistics.setLastTimeXiuMianCollNum(lastTimeXiuMianCollNum);
                /*
                 * 已链接休眠的献浆员
                 */
                Integer connXMPNum = xiuMianPList.size();
                statistics.setConnXMPNum(connXMPNum);
                /*
                 * 休眠中未链接献浆员
                 */
                Integer notConnXMPNum;
                /*
                 * 已链接休眠的献浆员同期献浆总量/袋
                 */
                Integer lastTimeConnXMCollNum = lastTimeXiuMianCollNum;
                statistics.setLastTimeConnXMCollNum(lastTimeConnXMCollNum);
                /*
                 * 休眠中未链接献浆员同期献浆总量/袋
                 */
                Integer lastTimeNotConnXMCollNum;

                //流失：去年没来和今年没来的交集
                List<Long> liuShiPList = (List<Long>) AdscmBaseUtils.getInstance().retainList(lastYearNotComedList,currentTimeNotComedPList);
                //流失浆员
                cacheTypeMap.put("liuShiPList",liuShiPList);
                /*
                 * 流失浆员总人数
                 */
                Integer liuShiPNum = liuShiPList.size();
                statistics.setLiuShiPNum(liuShiPNum);

                /*
                 * 已链接献浆员总人数
                 */
                Integer connedPNum = linkProviderCount.intValue();
                statistics.setConnedPNum(connedPNum);
                /*
                 * 义工总人数
                 */
                Integer volunteerNum = nurseLinkProviderStatisticsMapper.getLinkUseVolunteeCount(useNurseId.intValue());
                statistics.setVolunteerNum(volunteerNum);
                /*
                 * 义工管理浆员总人数
                 */
                Integer volunConnGJZNum = nurseLinkProviderStatisticsMapper.getLinkUseVolunteePCount(useNurseId.intValue());
                statistics.setVolunConnGJZNum(volunConnGJZNum);
                /*
                 * 忠诚浆员去年全年献浆量
                 */
                Integer lastYearZCCollNum;
                /*
                 * 进站但未被链接的浆员
                 */
                Integer comedNotConnPNum;
                /*
                 * 进站未链接浆员当期献浆量
                 */
                Integer comedNotConnThisTimeCollNum;
                /*
                 * 进站未链接浆员同期献浆量
                 */
                Integer comedNotConnLastTimeCollNum;
                /*
                 * 进站未链接浆员去年献浆总量
                 */
                Integer comedNotConnLastYearCollNum;


                //未处理的信息都是在链接护士统计中不需要统计，也不能统计的信息


                //找到R浆员、建档未献浆浆员、有五步链接的浆员，存入缓存中
                List<Long> rCardPList = nurseLinkProviderStatisticsMapper.getRCardPList(linkNursePList);
                //R浆员
                cacheTypeMap.put("rCardPList",rCardPList);

                endTime = nowDateStr + " 23:59:59";
                //有献浆记录的浆员
                List<Long> hasConnPList = nurseLinkProviderStatisticsMapper.getProviderComedPList(linkNursePList, "1997-01-01 00:00:00", endTime);
                List<Long> createButNotConnPList = (List<Long>) AdscmBaseUtils.getInstance().removeList(linkNursePList,hasConnPList);
                //建档未献浆浆员
                cacheTypeMap.put("createButNotConnPList",createButNotConnPList);

                //有五步链接的浆员
                List<Long> hasFiveLinkPList = nurseLinkProviderStatisticsMapper.getHasFiveLinkPList(linkNursePList);
                cacheTypeMap.put("hasFiveLinkPList",hasFiveLinkPList);



                cacheMap.put(useNurseId.intValue(),cacheTypeMap);
                if (isInsert){
                    nurseLinkProviderStatisticsMapper.insert(statistics);
                }else {
                    nurseLinkProviderStatisticsMapper.updateById(statistics);
                }


            }
            AdscmBaseUtils.getInstance().addObjToRedis("nurseLinkProviderStatisticsService.loadNurseLinkProviderStatistics.cacheMap",cacheMap,objectRedisTemplate,20, TimeUnit.HOURS);
        }





    }


    @Override
    public List<Map<String, String>> getLinkNursePCount() throws AdscmSystemException {

        logger.info("得到所有链接护士的链接数量。。。");
        List<Map<String, String>> result = new ArrayList<>();

        Wrapper<NurseLinkProviderStatistics> wrapper = new EntityWrapper<>();
        wrapper.setSqlSelect("nurseId","nurseName","allProviderNum");
        List<NurseLinkProviderStatistics> list = nurseLinkProviderStatisticsMapper.selectList(wrapper);

        if (AdscmBaseUtils.getInstance().listIsNotEmpty(list)){
            for (NurseLinkProviderStatistics statistics : list) {
                Map<String, String> map = new HashMap<>();
                map.put(statistics.getNurseId()+"",statistics.getNurseName()+"【"+statistics.getAllProviderNum()+"人】");
                result.add(map);
            }
        }
        return result;
    }

    @Override
    public Map<String, Map<String, Object>> getLinkNurseAllTypeProvider(Integer nurseId) throws AdscmSystemException {

        logger.info("根据护士，查询护士链接的所有浆员类型及浆员数量、根据村落分组的浆员数量。。。");

        Map<String, Map<String, Object>> result = new LinkedHashMap<>();

        Map<String, Map<String, List<Long>>> cacheMap = (Map<String, Map<String, List<Long>>>) objectRedisTemplate.opsForValue().get("nurseLinkProviderStatisticsService.loadNurseLinkProviderStatistics.cacheMap");

        if (cacheMap != null) {
            Map<String, List<Long>> cacheTypeMap = cacheMap.get(nurseId+"");
            //链接的所有浆员列表
            List<Long> allLinkProviderList = cacheTypeMap.get("allLinkProviderList");
            this.loadResultMap(allLinkProviderList,result,"所有浆员","allLinkProviderList");

            //今年已进站
            List<Long> thisTimeComedList = cacheTypeMap.get("thisTimeComedList");
            this.loadResultMap(thisTimeComedList,result,"A：今年已进站浆员","thisTimeComedList");

            //忠诚浆员
            List<Long> nowZcPList = cacheTypeMap.get("nowZcPList");
            this.loadResultMap(nowZcPList,result,"A1：忠诚浆员","nowZcPList");

            //复苏浆员
            List<Long> fusuPList = cacheTypeMap.get("fusuPList");
            this.loadResultMap(fusuPList,result,"A2：复苏浆员","fusuPList");

            //今年新浆员
            List<Long> thisTimeCreateList = cacheTypeMap.get("thisTimeCreateList");
            this.loadResultMap(thisTimeCreateList,result,"A3：今年新浆员","thisTimeCreateList");

            //今年未进站浆员
            List<Long> currentTimeNotComedPList = cacheTypeMap.get("currentTimeNotComedPList");
            this.loadResultMap(currentTimeNotComedPList,result,"B：今年未进站浆员","currentTimeNotComedPList");

            //休眠浆员
            List<Long> xiuMianPList = cacheTypeMap.get("xiuMianPList");
            this.loadResultMap(xiuMianPList,result,"B1：休眠浆员","xiuMianPList");


            //流失浆员
            List<Long> liuShiPList = cacheTypeMap.get("liuShiPList");
            this.loadResultMap(liuShiPList,result,"B2：流失浆员","liuShiPList");

            //R浆员
            List<Long> rCardPList = cacheTypeMap.get("rCardPList");
            this.loadResultMap(rCardPList,result,"R浆员","rCardPList");

            //建档未献浆浆员
            List<Long> createButNotConnPList = cacheTypeMap.get("createButNotConnPList");
            this.loadResultMap(createButNotConnPList,result,"建档未献浆浆员","createButNotConnPList");


            //有五步链接的浆员
            List<Long> hasFiveLinkPList = cacheTypeMap.get("hasFiveLinkPList");
            this.loadResultMap(hasFiveLinkPList,result,"有五步链接的浆员","hasFiveLinkPList");
        }
        return result;
    }

    /**
     *
      * @param providerList
     * @param result
     * @param typeDesc
     */
    private void loadResultMap(List<Long> providerList,Map<String, Map<String, Object>> result,String typeDesc,String resultKey){
        Map<String, Object> value = new TreeMap<>();
        value.put("typeDesc",typeDesc + "【"+providerList.size()+"人】");
        //村落集合
        List<Map<String,String>> villList = new ArrayList<>();
        if (AdscmBaseUtils.getInstance().listIsNotEmpty(providerList)){
            List<Map<String,String>> tempList =  nurseLinkProviderStatisticsMapper.selectPCountGroupByVill(providerList);
            Map<String, String> tempMap = new TreeMap<>();
            tempMap.put("splitAddress","所有村落");
            tempMap.put("villProviderCount",tempList.size()+"");
            tempMap.put("villCountDesc","所有村落【"+tempList.size()+"个村】");
            villList.add(0,tempMap);
            villList.addAll(1,tempList);
        }else {
            Map<String, String> tempMap = new TreeMap<>();
            tempMap.put("splitAddress","所有村落");
            tempMap.put("villProviderCount","0");
            tempMap.put("villCountDesc","所有村落【0个村】");
            villList.add(0,tempMap);
        }
        value.put("villList",villList);
        result.put(resultKey,value);
    }


    @Override
    public List<ProviderRoundBase> getProviderRoundByCondition(Integer nurseId, String type, String villName) throws AdscmSystemException {

        logger.info("根据护士、浆员类型及村落名称，查询浆员的轮次信息...");

        if (nurseId != null && nurseId > 0){}else {
            throw new AdscmSystemException("参数【nurseId】为 Null ，查询失败");
        }
        if (!AdscmBaseUtils.getInstance().isNotEmpty(type,null)){
            type = "allLinkProviderList";
        }

        Map<String, Map<String, List<Long>>> cacheMap = (Map<String, Map<String, List<Long>>>) objectRedisTemplate.opsForValue().get("nurseLinkProviderStatisticsService.loadNurseLinkProviderStatistics.cacheMap");

        List<ProviderRoundBase> list = null;

        if (cacheMap != null) {

            Integer currentYear = AdscmDateUtils.getTheYear(new Date());
            Integer lastYear = currentYear - 1;

            Map<String, List<Long>> cacheTypeMap = cacheMap.get(nurseId+"");
            //链接的所有浆员列表
            List<Long> providerList = cacheTypeMap.get(type);

            if (AdscmBaseUtils.getInstance().listIsNotEmpty(providerList)){
                list = providerRoundBase2Mapper.selectVillProviderRound(providerList,villName,currentYear,lastYear);
                //添加管理护士、义工、最新沟通内容
                if (AdscmBaseUtils.getInstance().listIsNotEmpty(list)){
                    for (ProviderRoundBase providerRoundBase : list) {
                        providerRoundBaseService.getNurseYg(providerRoundBase);
                    }
                }
            }else {
                list = new ArrayList<>();
            }
        }
        return list;
    }
}
